<?php

class mdl_t_surat_masuk extends CI_Model{

	function __construct(){
		parent::__construct();
	}

	function getData($num=0, $offset=0){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('t_surat_masuk');
		$this->db->limit($num, $offset);
		$this->db->order_by('id');

		return $this->db->get();

	}

	function insert($data){
		$this->db->flush_cache();
		$this->db->set('kode', $data['kode']);
		$this->db->set('no_agenda', $data['no_agenda']);
		$this->db->set('indek_berkas', $data['indek_berkas']);
		$this->db->set('isi_ringkas', $data['uraian']);
		$this->db->set('dari', $data['dari']);
		$this->db->set('no_surat', $data['no_surat']);
		$this->db->set('tgl_surat', $data['tgl_surat']);
		$this->db->set('tgl_diterima', $data['tgl_diterima']);
		$this->db->set('kpd_yth',  $data['kpd_yth']);
		$this->db->set('keterangan',  $data['keterangan']);
		$this->db->set('file',  $data['file']);
		$this->db->set('pengolah',  $data['pengolah']);
		$this->db->set('is_surat_masuk',  $data['is_surat_masuk']);
		//$this->db->set('status',  $data['status']);
		$this->db->set('userid',  $data['userid']);

		$result = $this->db->insert('t_surat_masuk');

		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}

	function getDataEdit($id){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('t_surat_masuk');
		$this->db->where('id', $id);

		return $this->db->get();
	}


	function update($data){
		$this->db->flush_cache();
		$this->db->set('status', $data['status']);
		$this->db->where('id', $data['id_surat']);
		$result = $this->db->update('t_surat_masuk');

		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}

	}

	function delete($data){
		$this->db->flush_cache();
		$this->db->where('id', $data['id']);
		$result = $this->db->delete('t_surat_masuk');

		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}

	}

	function get_union_table_all_row($awal, $akhir,$arr_where='', $get_count=false){
		/*$this->db->select("c.id_surat AS id, b.id_disposisi, c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status");
	    //$this->db->distinct();
	    $this->db->from("t_surat_masuk a");
	    $this->db->join("t_disposisi b","a.id = b.id_surat");
	    $this->db->join("t_status_disposisi c","b.id_disposisi = c.id_disposisi","left");
	    $this->db->get();
	    $query1 = $this->db->last_query();

	    $this->db->select("a.id, b.id_disposisi, a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status");
	    $this->db->join("t_disposisi b","a.id = b.id_surat");
	    //$this->db->distinct();
	    $this->db->from("t_surat_masuk a");
	    //$this->db->where_in("id",$model_ids);
	    $this->db->get();
	    $query2 =  $this->db->last_query();

	    $query = $this->db->query($query1." UNION ALL2 ".$query2);

	    return $query->result();*/
	    $limit_row = "";
	    if ($get_count==true){
	    	$limit_row = "LIMIT $awal, $akhir";
	    }
	    if ($arr_where!=''){

	    }

	    $query = $this->db->query("SELECT id, id_disposisi, is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status,kd_unit,id_status_disposisi
								FROM (
								SELECT c.id_surat AS id, c.id_disposisi, c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,c.kd_unit,c.id as id_status_disposisi
								FROM t_surat_masuk a
								INNER JOIN t_disposisi b ON a.id = b.id_surat
								LEFT JOIN t_status_disposisi c ON b.id_disposisi = c.id_disposisi
								UNION ALL
								SELECT a.id, '', a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,'',''
								FROM t_surat_masuk a
								)tbl
	    						" . $arr_where . "
								ORDER BY id, is_surat_masuk DESC
								" . $limit_row);

	    return $query;

	}

	function get_union_table_inner($awal, $akhir,$arr_where='', $get_count=false){
	    $limit_row = "";
	    if ($get_count==true){
	    	$limit_row = "LIMIT $awal, $akhir";
	    }
	    if ($arr_where!=''){

	    }

	    $query = $this->db->query("SELECT id, id_disposisi, is_surat_masuk,kode,no_agenda,isi_ringkas,file,dari,tgl_surat,no_surat,status,id_status_disposisi
								FROM (
								SELECT c.id_surat AS id, c.id_disposisi, c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,c.id as id_status_disposisi
								FROM t_surat_masuk a
								INNER JOIN t_disposisi b ON a.id = b.id_surat
								LEFT JOIN t_status_disposisi c ON b.id_disposisi = c.id_disposisi
								UNION ALL
								SELECT a.id, '', a.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status,''
								FROM t_surat_masuk a
								)tbl
	    						" . $arr_where . "
								ORDER BY id, is_surat_masuk DESC
								" . $limit_row);

	    return $query;

	}

	function get_detail($data){
		$this->db->flush_cache();
		$this->db->select('a.id AS id, c.id_disposisi, c.is_surat_masuk,a.kode,a.no_agenda,a.isi_ringkas,a.file,a.dari,a.tgl_surat,a.no_surat,a.status');
		$this->db->from('t_surat_masuk a');
		$this->db->join('t_disposisi b','a.id = b.id_surat','left');
		$this->db->join('t_status_disposisi c','b.id_disposisi = c.id_disposisi','left');
		$this->db->where('a.id', $data['id_surat']);
		if ($data['id_disposisi']!='0'){
			$this->db->where('c.id_disposisi', $data['id_disposisi']);
		}
		if ($data['id_status_disposisi']!='0'){
			$this->db->where('c.id', $data['id_status_disposisi']);
		}


		return $this->db->get();
	}


}
?>